package mc.midlet;
import client.*; 
import server.*; 
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Kommunikation extends MIDlet implements CommandListener {

	private Display d;
	private Command exit;
	private Command client;
	private Command server; 
	private Form f;
	public Kommunikation() {
		
		this.d= Display.getDisplay(this);
		
		this.f= new Form ("Bluetooth");
		
		this.exit = new Command ("Exit", Command.EXIT,1);
		this.f.addCommand(this.exit);
			
		this.client = new Command ("Client", Command.ITEM, 0 ); 
		this.f.addCommand( this.client ); 
		
		this.server = new Command( "Server", Command.ITEM, 1 ); 
		this.f.addCommand( this.server ); 
		this.f.setCommandListener( this ); 
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		this.d.setCurrent(f);

	}

	public void commandAction(Command cmd, Displayable d) {
		if (cmd== this.exit)
		{
			try
			{
				this.destroyApp(false);
				this.notifyDestroyed();
			}
			catch( MIDletStateChangeException ex )
			{
				ex.printStackTrace(); 
			}
		}
		else if( cmd == this.client )
		{
			 this.startClient(); 
		}
		else if( cmd == this.server )
		{
			this.startServer(); 
		}
	}
	private void startClient()
	{
		BtClient client = null; 
		
		client = new BtClient("Bluetooth - Client" ); 
		this.d.setCurrent( client ); 
		client.start(); 
	}
	private void startServer()
	{
		BtServer server = null; 
		
		server = new BtServer( "Bluetooth - Server" ); 
		this.d.setCurrent( server ); 
		server.start(); 
		
	}
}
